#ifndef __SEARCHER_H__
#define __SEARCHER_H__
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include "cppjieba/Jieba.hpp"

namespace searcher{

  struct DocInfo{
    std::string title;
    std::string content;
    std::string url;
    uint64_t doc_id;
  };

  struct Weight{
    uint64_t doc_id;//文档id
    int weight;//权重
    std::string key;//关键词
  };

  typedef std::vector<Weight> InvertedList;
  //索引模块
  class Index{
  private:
    std::vector<DocInfo> forward_index_;//正排索引
    std::unordered_map<std::string, InvertedList> inverted_index_;//倒排索引
    cppjieba::Jieba jieba_;
  public:
    Index();
    bool Build(const std::string&);
    const DocInfo* GetDocInfo(uint64_t doc_id) const;
    const InvertedList* GetInvertedList(const std::string& key) const;
    void CutWord(const std::string& input, std::vector<std::string>* output);
  private:
    const DocInfo* BuildForward(const std::string& line);
    void BuildInverted(const DocInfo& doc_info);
  };

  //搜索模块
  class Searcher{
      private:
        Index* index_;
      public:
        Searcher():index_(new Index()){

        }

        ~Searcher()
        {
          if(index_) delete index_;
        }

        bool Init(const std::string& input_path);
        
        bool Search(const std::string& query, std::string* result);

      private:
        std::string GetDesc(const std::string& content, const std::string& key);
  };

}//end namespace
#endif
